/** Concrete implementation for ISessionService
 *
 *  @author Scott Monahan
 *  @version $Id$
 *  
 *  File last changed on $Date:$ by $Author:$ in $Rev:$
 *
 */

package info637.pprc.services;

import java.util.List;

import info637.pprc.model.Session;
import info637.pprc.repository.ISessionRepository;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "sessionService")
public class SessionService implements ISessionService {

	static Logger log = Logger.getLogger(SessionService.class.getName());
	
	@Autowired
	private ISessionRepository sessionRepository = null;
	
	@Override
	public void checkForTimeouts() {
		
		log.debug("Checking for timeouts ...");
		
		List<String> expiredSessions = sessionRepository.expireSessions(5 * 60 * 1000);
		
		for (String sessionId : expiredSessions) {
			
			log.info("Expiring Session: [" + sessionId + "]");
		}
	}

	@Override
	public boolean isSessionValid(String sessionId) {

		Session session = this.sessionRepository.findBySessionId(sessionId);
		
		if (session == null) {
			
			return false;
			
		} else {
			
			return true;
		}
	}

	@Override
	public Session createSession() {

		return this.sessionRepository.createNewSession();
	}

	@Override
	public void updateLastAccessed(String sessionId) {
		
		this.sessionRepository.updateLastAccessed(
			this.sessionRepository.findBySessionId(sessionId)
		);
	}
}
